package interfaces.hud.pilot;

import java.util.HashMap;
import java.util.Iterator;

import org.fenggui.StandardWidget;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.util.Color;

import interfaces.GUISource;
import interfaces.superWidgets.FlowContent;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.StaticContent;
import interfaces.superWidgets.UpdateWidget;

public class KillInfoContent extends FlowContent implements UpdateWidget {
	
	private static final float MAX_TIME = 5f;
	
	private HashMap<StandardWidget, Float> widgetMap;
	
	public KillInfoContent(int width, int height, int x, int y, int widgetHeight) {
		super(width, height, x, y, widgetHeight);
		
		getAppearance().add(new PlainBackground(Color.TRANSPARENT));
		
		widgetMap = new HashMap<StandardWidget, Float>();
	}
	
	public void add(String[] text, Color[] colors, int money) {
		assert(text.length == colors.length);
		
		int width = getWidth();
		int height = getWidgetHeight();
		int labelWidth = width / (text.length + 1);
		
		StaticContent content = new StaticContent(width, height, 0, 0);
		
		ImageFont font = GUISource.smallestFont;
		
		for(int i = 0; i < text.length; i++) {
			InterfaceLabel label = new InterfaceLabel(text[i], font);
			GUISource.setTextColor(label, colors[i]);
			label.setXY(i * labelWidth, 0);
			label.setSize(labelWidth, height);
			content.addWidget(label);
		}
		
		InterfaceLabel moneyLabel = new InterfaceLabel(" + " + money, font);
		GUISource.setTextColor(moneyLabel, Color.LIGHT_GREEN);
		moneyLabel.setXY(labelWidth * text.length, 0);
		moneyLabel.setSize(labelWidth, height);
		content.addWidget(moneyLabel);
		
		addFlowWidget(content);
		widgetMap.put(content, new Float(0f));
	}
	
	@Override
	public void update(float time) {
		Iterator<StandardWidget> iter = widgetMap.keySet().iterator();
		while(iter.hasNext()) {
			StandardWidget w = iter.next();
			float t = widgetMap.get(w).floatValue();
			if(t > MAX_TIME) {
				iter.remove();
				removeWidget(w);
				widgets.remove(w);
			} else widgetMap.put(w, new Float(t + time));
		}
	}
}